package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.security.PublicKey;
import java.util.ArrayList;
import java.util.List;

//套餐
@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    //分页查找
    @Override
    public Page<Setmeal> findPage(Integer pageNum, Integer pageSize, String name) {
        //分页查找
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        //根据名字查询
        qw.like(StrUtil.isNotEmpty(name), Setmeal::getName, name);
        //分页条件
        Page<Setmeal> page = new Page<>(pageNum, pageSize);
        //分页查询
        page = setmealMapper.selectPage(page, qw);
        //查询套餐
        for (Setmeal setmeal : page.getRecords()) {
            // 2. 根据category_id 查询并封装分类名称
            Category category = categoryMapper.selectById(setmeal.getCategoryId());
            setmeal.setCategoryName(category.getName());
            // 3. 根据id查询并封装菜品列表
            LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<SetmealDish>();
            wrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
            List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);
            setmeal.setSetmealDishes(setmealDishList);
        }
        return page;
    }

    //新增
    @Override
    public void save(Setmeal setmeal) {
        //将前端传来的数据添加到数据库中
        setmealMapper.insert(setmeal);
        //遍历前端提交套餐菜品列表
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        //遍历列表
        for (SetmealDish setmealDish : setmealDishes) {
            //将集合中的id存到套餐ID中
            setmealDish.setSetmealId(setmealDish.getId());
            //将集合中的名字存到套餐名字中
            setmealDish.setName(setmealDish.getName());
        }
    }

    //Id回显
    @Override
    public Setmeal findById(long id) {
        Setmeal setmeal = setmealMapper.selectById(id);

        //根据id查询套餐下的菜品
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(qw);

        //存入套餐
        setmeal.setSetmealDishes(setmealDishes);
        return setmeal;
    }

    //修改
    @Override
    public void updateSetmeal(Setmeal setmeal) {
        setmealMapper.selectById(setmeal);
        //删除旧菜品
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId, setmeal.getId());
        setmealDishMapper.delete(qw);
        //添加新菜品
        for (SetmealDish setmealDish : setmeal.getSetmealDishes()) {
            setmealDish.setSetmealId(setmeal.getId());
            //添加到数据库
            setmealDishMapper.insert(setmealDish);

        }
    }

    //更改起售-停售
    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        //先找出id一样的套餐
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId, ids);
        //将状态修改成前端传来的状态
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        //修改
        setmealMapper.update(setmeal, qw);
    }

    //删除
    @Override
    public void deleteBatch(List<Long> ids) {
        // 1.先查询套餐状态，是否有启售 select count(*) from setmeal where status=1 and id in(1,3,5);
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId, ids);
        qw.eq(Setmeal::getStatus, 1);
        Integer count = setmealMapper.selectCount(qw);

        //如果查到的数据超过0,则不让删除
        if (count > 0) {
            throw new CustomException("套餐状态有启售!不可以删除");
        }

        // 2.删套餐
        setmealMapper.deleteBatchIds(ids);
        // 3.删除套餐内菜品中间表  delete from setmeal_dish where setmeal_id in(1,2,3)
        LambdaQueryWrapper<SetmealDish> qw2 = new LambdaQueryWrapper<>();
        qw2.in(SetmealDish::getSetmealId,ids);
        setmealDishMapper.delete(qw2);
    }

    @Override
    public List<Setmeal> list(Long categoryId, Integer status) {

        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId,categoryId);
        qw.eq(Setmeal::getStatus,status);
        return setmealMapper.selectList(qw);
    }

    //根据套餐ID回显菜品
    @Override
    public List<Dish> dish(Long id) {
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();// 查询第三张表
        qw.eq(SetmealDish::getSetmealId,id);// 查询第三张表的套餐id
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(qw);

        ArrayList<Dish> dishList = new ArrayList<>();// 因为返回的泛型是Dish,所以创建Dish集合

        for (SetmealDish setmealDish : setmealDishes) {// 遍历第三张表查询到的套餐和菜品
            Dish dish = dishMapper.selectById(setmealDish.getDishId()); // 根据第三张表的菜品ID,去Dish表查询
            //数量
            dish.setCopies(setmealDish.getCopies());// 菜品的数量,去第三张表的数量赋值
            dishList.add(dish); // 将菜品添加到集合中
        }
        return dishList; // 返回 <Dish> 泛型的集合
    }
}


















